/**
 * 
 */
package com.gmail.blackdog1987.carcare.network.response.handler;

import java.util.Date;

import org.json.JSONObject;

import com.gmail.blackdog1987.carcare.entity.TGasCard;
import com.gmail.blackdog1987.carcare.network.ResponseCustomError;
import com.gmail.blackdog1987.carcare.network.response.GasValidCardResponse;
import com.gmail.blackdog1987.carcare.network.response.IResponse;
import com.gmail.blackdog1987.carcare.util.LogUtil;

/**
 * @author wdy
 *
 */
public class GasValidCardResponseHandler extends BaseResponseHandler {

	public static final String TAG = GasValidCardResponseHandler.class.toString();
	/** 监听类 */
	private OnResponseListener mListener;
	
	private GasValidCardResponse mResponse;
	
	public void setOnResponseListener(OnResponseListener l) {
		this.mListener = l;
	}

	@Override
	public void onFailure(String responseBody, Throwable error) {
		mResponse = new GasValidCardResponse();
		if(!checkConn(error)) {
			mResponse.error = IResponseHandler.CONNECTION_ERROR;
		} else {
			if(responseBody != null) {
				LogUtil.e(TAG, responseBody);
				mResponse.error = new ResponseCustomError(responseBody);
			} else {
				LogUtil.e(TAG, responseBody);
				mResponse.error = IResponseHandler.CONNECTION_ERROR;
			}
		}
		
		if(mListener != null) {
			mListener.onError(this);
		}
	}
	
	@Override
	public void onSuccess(JSONObject response) {
		boolean result = this.parseJson(response);
		if(mListener == null) {
			return;
		}
		
		if(!result) {
			mListener.onError(this);
		} else {
			mListener.onResponse(this);
		}
	}
	
	public boolean parseJson(Object response) {
		mResponse = new GasValidCardResponse();
		try {
			if(response != null) {
				LogUtil.d(GasValidCardResponseHandler.class.toString(), response.toString());
				// 错误信息处理
				JSONObject json = (JSONObject)response;
				if(!(Boolean) json.get("success")) {
					mResponse.error = new ResponseCustomError(json.toString());
					return false;
				}
				
				JSONObject res = (JSONObject) response;
				JSONObject result = res.getJSONObject("record");
				if(result == null|| result.length()==0){
					return false;
				}
				mResponse.card = new TGasCard();
				mResponse.card.address = result.optString("address");
				mResponse.card.code = result.optString("code");
				mResponse.card.createtime = result.optLong("createtime");
				mResponse.card.id = result.optInt("id");
				mResponse.card.idcard = result.optString("idcard");
				mResponse.card.idcardPic = result.optString("idcardPic");
				mResponse.card.idcardPic2 = result.optString("idcardPic2");
				mResponse.card.name = result.optString("name");
				mResponse.card.phone = result.optString("phone");
				mResponse.card.price = result.optDouble("price");
				mResponse.card.status = result.optInt("status");
			}
		} catch (Exception e) {
			LogUtil.e(GasValidCardResponseHandler.class.toString(), e.getMessage());
			mResponse.error = IResponseHandler.JSON_PARSE_ERROR;
			if(checkWLAN(response)) {
				mResponse.error = IResponseHandler.CONNECTION_ERROR;
			}
		}
		return true;
	}

	@Override
	public IResponse getResponse() {
		return this.mResponse;
	}
}
